<template>
  <a-drawer
    :title="title"
    :closable="false"
    :maskClosable="false"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :md="24" :sm="24">
            <a-form-item
              label="教室名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入教室名称" v-decorator="['name', {rules: [{required: true, message: '请输入教室名称！'}]}]"
                       :disabled="id!=''"/>
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24">
            <a-form-item
              label="排序号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input-number placeholder="请输入排序号"
                              v-decorator="['sortNum', {rules: [{required: true, message: '请输入排序号！'}]}]"/>
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24">
            <a-form-item
              label="备注"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-textarea aria-rowcount="3" placeholder="请输入备注" v-decorator="['remark']"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
    <div class="drawer-footer">
      <a-button class="left" @click="handleCancel">
        关闭
      </a-button>
      <a-button class="right" type="primary" @click="handleSubmit">
        提交
      </a-button>
    </div>
  </a-drawer>
</template>

<script>
  import { sysClassroomAdd, sysClassroomEdit } from '@/api/modular/system/sysClassroomManage'

  export const dateFormat = 'YYYY-MM-DD'
  export default {
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        id: '',
        title: ''
      }
    },
    created() {

    },
    methods: {
      // 初始化方法
      open(record) {
        if (record) {
          this.id = record.id
          this.title = '编辑教室信息'
          setTimeout(() => {
            this.form.setFieldsValue(
              {
                id: record.id,
                sortNum: record.sortNum,
                name: record.name,
                remark: record.remark
              }
            )
          }, 100)
        } else {
          this.id = ''
          this.title = '新增教室信息'
        }
        this.visible = true
      },
      handleSubmit() {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            console.log(values)
            const data = { ...values }
            if (this.id) {
              data.id = this.id
              sysClassroomEdit(data).then((res) => {
                if (res.success) {
                  this.$message.success('更新教室信息成功')
                  this.confirmLoading = false
                  this.$emit('ok', data)
                  this.handleCancel()
                } else {
                  this.$message.error('更新系教室信息失败：' + res.message)
                }
              }).finally((res) => {
                this.confirmLoading = false
              })
            } else {
              sysClassroomAdd(data).then((res) => {
                if (res.success) {
                  this.$message.success('新增教室信息成功')
                  this.confirmLoading = false
                  this.$emit('ok', data)
                  this.handleCancel()
                } else {
                  this.$message.error('新增教室信息失败：' + res.message)
                }
              }).finally((res) => {
                this.confirmLoading = false
              })
            }
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel() {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
